home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / api_shar / sharea.pas < prev    next >
Pascal/Delphi Source File  |  1989-01-25  |  5KB  |  202 lines

  1. (****************************************************************
  2. *
  3. *  Name:          SHAREA
  4. *
  5. *  Function:      share memory/data among multiple processes
  6. *
  7. *  Shows how to:  1. allocate and deallocate shared memory.
  8. *                 2. read from and write to shared memory.
  9. *                 3. mail to another process the address of shared data.
  10. *                 4. control access to shared data via mailbox semaphore.
  11. *
  12. *  Written by:    Larry Rush, Quarterdeck Office Systems
  13. *
  14. *  Contact:       Voice:  (213) 392-9851, (213) 392-9701
  15. *                 BBS:    (213) 396-3904, (213) 392-2278
  16. *                 Fax:    (213) 399-3802
  17. *
  18. ****************************************************************)
  19.  
  20. program ShareA;
  21.  
  22. uses DVAPI;
  23.  
  24. const
  25.  
  26.   (* minimum API version required *)
  27.   REQUIRED = $200;
  28.  
  29.   (* PIF-related constants *)
  30.   PIFNAME = 'SHAREB.DVP';
  31.   PIFLEN = 416;
  32.  
  33.   (* arbitrary # times to read/write shared memory *)
  34.   REPS = 4;
  35.  
  36. var
  37.  
  38.   (* API version number *)
  39.   version : integer;
  40.  
  41.   (* TFDD text file *)
  42.   tfd : text;
  43.  
  44.   (* PIF-related variables *)
  45.   fp : file;
  46.   dvpbuf : array [0..415] of char;
  47.   pathbuf : string;
  48.  
  49.   (* application handle of other process *)
  50.   apphanb : ULONG;
  51.  
  52.   (* mail-related variables *)
  53.   error : integer;
  54.  
  55.   (* read/write loop control variable *)
  56.   i : integer;
  57.  
  58. type
  59.  
  60.   (* type declarations related to shared data *)
  61.   (*i* DATATYPE = integer; *i*)
  62.   DATATYPE = string[10];
  63.   DATAPTR = ^DATATYPE;
  64.   (*r* DATATYPE = record *r*)
  65.     (*r* link : DATAPTR; *r*)
  66.     (*r* lng : integer; *r*)
  67.     (*r* data : string[10]; *r*)
  68.   (*r* end; *r*)
  69.  
  70. const
  71.  
  72.   (* constant value to be assigned to shared memory *)
  73.   (*i* SHRCONST : DATATYPE = 11111; *i*)
  74.   SHRCONST : DATATYPE = 'AAAAA     ';
  75.   (*r* SHRCONST : DATATYPE = ( *r*)
  76.     (*r* link : Nil; *r*)
  77.     (*r* lng : 11111; *r*)
  78.     (*r* data : 'AAAAA     ' *r*)
  79.   (*r* ); *r*)
  80.  
  81. var
  82.  
  83.   (* pointer to shared data *)
  84.   bufptr : DATAPTR;
  85.  
  86.   (* mailbox semaphore controlling access to shared memory *)
  87.   sema : ULONG;
  88.  
  89. const
  90.  
  91.   (* global name of mailbox semaphore *)
  92.   name : string = 'Shared Memory Semaphore';
  93.  
  94.  
  95. (********************************************************************
  96. *  program_body  -  read, display and modify contents of shared data.
  97. ********************************************************************)
  98.  
  99. procedure program_body;
  100. begin
  101.  
  102.   (* open TFDD *)
  103.   tfd_open (tfd,win_me);
  104.  
  105.   (* read other process' dvp file into buffer area *)
  106.   assign (fp,PIFNAME);
  107.   reset (fp,PIFLEN);
  108.   blockread (fp,dvpbuf,1);
  109.   close (fp);
  110.  
  111.   (* move current drive/path into DVP buffer *)
  112.   getdir (0,pathbuf);
  113.   dvpbuf[100] := pathbuf[1];
  114.   move (pathbuf[3],dvpbuf[101],length(pathbuf)-2);
  115.  
  116.   (* start other process & get its task handle *)
  117.   apphanb := app_start (@dvpbuf,PIFLEN);
  118.  
  119.   (* create & name mailbox semaphore *)
  120.   sema := mal_new;
  121.   mal_sname (sema,name);
  122.  
  123.   (* allocate shared memory & get its buffer pointer *)
  124.   bufptr := api_getmem (sizeof (DATATYPE));
  125.  
  126.   (* copy initial data into shared memory *)
  127.   bufptr^ := SHRCONST;
  128.  
  129.   (* mail to other process the pointer to shared data *)
  130.   error := mal_write (mal_of (apphanb),@bufptr,sizeof (bufptr));
  131.  
  132.   (* disallow closing of window *)
  133.   win_disallow (win_me,ALW_CLOSE);
  134.  
  135.   (* loop till handle of other process is no longer valid *)
  136.   while (api_isobj (apphanb)) do
  137.   begin
  138.  
  139.     (* lock semaphore *)
  140.     mal_lock (sema);
  141.  
  142.     (* loop REPS times *)
  143.     for i := 1 to REPS do
  144.     begin
  145.  
  146.       (* read & display current contents & address of shared data *)
  147.       (*i* writeln (tfd,bufptr^,' at ',seg (bufptr^),':',ofs (bufptr^)); *i*)
  148.       writeln (tfd,bufptr^,' at ',seg (bufptr^),':',ofs (bufptr^));
  149.       (*r* with bufptr^ do *r*)
  150.         (*r* writeln (tfd,lng,' ',data,' at ',seg (bufptr^),':',ofs (bufptr^)); *r*)
  151.  
  152.       (* modify contents of shared data *)
  153.       bufptr^ := SHRCONST;
  154.  
  155.     end;
  156.  
  157.     (* unlock semaphore *)
  158.     mal_unlock (sema);
  159.  
  160.   end;
  161.  
  162.   (* allow closing of window *)
  163.   win_allow (win_me,ALW_CLOSE);
  164.  
  165.   (* free allocated shared memory *)
  166.   api_putmem (bufptr);
  167.  
  168.   (* free allocated object *)
  169.   mal_free (sema);
  170.  
  171.   (* close TFDD *)
  172.   tfd_close (tfd);
  173.  
  174. end;
  175.  
  176.  
  177. (**********************************************************************
  178. *  main  -  check for DESQview present and enable required extensions.
  179. ***********************************************************************)
  180.  
  181. begin
  182.  
  183.   (* initialize Pascal interfaces and get API version number *)
  184.   version := api_init;
  185.  
  186.   (* if DESQview is not running or version is too low, display a message *)
  187.   if (version < REQUIRED) then
  188.     writeln ('This program requires DESQview version ',REQUIRED div 256,
  189.        '.',(REQUIRED mod 256) div 16,(REQUIRED mod 256) mod 16,' or later.')
  190.  
  191.   (* tell DESQview what extensions to enable and start application *)
  192.   else
  193.   begin
  194.     api_level (REQUIRED);
  195.     program_body;
  196.   end;
  197.  
  198.   (* disable Pascal interfaces and return from program *)
  199.   api_exit;
  200.  
  201. end.
  202.